package com.example.zhouzhaocheng;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.AbsCallback;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.apache.commons.lang3.StringEscapeUtils;

public class MainActivity1 extends AppCompatActivity {
    //    private String url = "https://tianqiapi.com/api";
    private String url = "https://tianqiapi.com/api?version=v1&appid=65716696&appsecret=pPQF7EgL&city=";
    private EditText cityE;
    private EditText weather;
    private RecyclerView recyclerView;
    private BaseRecyclerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        cityE = findViewById(R.id.cityE);
        weather = findViewById(R.id.weather);
        recyclerView = findViewById(R.id.recyclerView);
        weather.setText("默认天气信息\n\n" +
                "城市：崇左（chongzuo）\n" +
                "天气：25℃~32℃，多云\n" +
                "风向：东南风\n" +
                "空气：良，PM2.5值为55\n" +
                "提示：空气质量良好，可以外出，除极少天气污染物特别敏感的人群外对公众没有危害"
        );
    }

    private void postByOkGo(String url) {
        OkGo.<String>get(url)
                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String newValue = StringEscapeUtils.unescapeJava(response.body());
                        weather.setText(newValue);
                        try {
                            WeatherBean localBean =new Gson().fromJson(newValue,WeatherBean.class);
                            weather.setVisibility(View.GONE);
                            recyclerView.setVisibility(View.VISIBLE);
                            recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity1.this));
                            recyclerView.setAdapter(adapter);
                            adapter = new BaseRecyclerAdapter() {
                                @Override
                                protected void onBindView(@NonNull BaseViewHolder holder, @NonNull int position) {
                                    WeatherBean.Datum datum = localBean.data.get(position);
                                    TextView name = holder.getView(R.id.textView);
                                    TextView name2 = holder.getView(R.id.textView2);
                                    name.setText(datum.tem2+"~"+datum.tem1+"，"+datum.wea);
                                    name2.setText(datum.day);
                                }

                                @Override
                                protected int getLayoutResId(int position) {
                                    return R.layout.item_la;
                                }

                                @Override
                                public int getItemCount() {
                                    return localBean.data.size();
//                                    return 0;
                                }
                            };
                        }catch (Throwable e0){
                            Log.e("ywh",e0.getMessage());
                        }

                    }
                });
    }

    public void search(View view) {
        postByOkGo(url + cityE.getText().toString());
    }
}